Imports System.Collections

Partial Class pages_Files_default : Inherits CPage

#region "Querystring (Filters)"
    Public ReadOnly Property Search As String
        Get
            Return CWeb.RequestStr("search")
        End Get
    End Property
    
    'Rename or Delete:
    Public ReadOnly Property ExtensionId As Integer
        Get
            Return CWeb.RequestInt("extensionId")
        End Get
    End Property
    Public ReadOnly Property ExtensionIdAsByte As Byte?
        Get
            If Integer.MinValue = ExtensionId Then Return Nothing
            Return ExtensionId '0 for no value set, int.min for all
        End Get
    End Property
#End Region

#Region "Data"
    Public ReadOnly Property [Files]() As CFileList
        Get
            Return CFile.Cache.Search(txtSearch.Text, ExtensionIdAsByte) 'Paging done in usercontrol
        End Get
    End Property
#End Region

#Region "Event Handlers - Page "
    Protected Overrides Sub PageInit()
        'Populate Dropdowns
        AddMenuSide("Files")
        AddMenuSide("Duplicates", CSitemap.Duplicates)
        AddMenuSide("Missing", CSitemap.FilesMissing(ExtensionId))

        ddExt.DataSource = CExtension.Cache
        ddExt.DataBind()
        CDropdown.BlankItem(ddExt, "-- All Extensions --")
        CDropdown.BlankItem(ddExt, "* None Set ", 0)
        CDropdown.SetValue(ddExt, ExtensionId)
    
        'Search state (from querystring)
        txtSearch.Text = Me.Search
        
        'Display Results
        ctrlFiles.Display(Me.Files, True)

        'Client-side
        Me.Form.DefaultFocus  = txtSearch.ClientID
        Me.Form.DefaultButton = btnSearch.UniqueID  'CTextbox.OnReturnPress(txtSearch, btnSearch)
    End Sub
#End Region

#Region "Event Handlers - Form"
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click, ddExt.SelectedIndexChanged
        Response.Redirect(CSitemap.Files(txtSearch.Text, CDropdown.GetInt(ddExt)))
    End Sub

    Private m_count As Integer = 0
    Protected Sub btnCheckAll_Click(sender As Object, e As System.EventArgs) Handles btnCheckAll.Click

        For Each i As CFile In Files

            If Long.MinValue = i.FileSize Then
                Response.Write(i.FileName & " - SKIPPED<br>")
                Response.Flush()
                Continue For
            End If

            m_count += 1

            Dim wc As New Net.WebClient
            AddHandler wc.DownloadDataCompleted, AddressOf DownloadCompleted
            wc.DownloadDataAsync(New Uri(CConfig.DropboxUrl(i.FileName)), i)
        Next

        For i = 1 To 50
            If 0 <> m_count Then Threading.Thread.Sleep(500) Else Exit For
        Next

        Response.End()
    End Sub
    Private Sub DownloadCompleted(sender As Object, e As Net.DownloadDataCompletedEventArgs)
        Dim i As CFile = e.UserState
        SyncLock Response
            If Not IsNothing(e.Error) Then
                i.FileSize = Long.MinValue
                i.Save()
                Response.Write(i.FileNameAndSize & "FILE NOT FOUND: " & e.Error.Message & "<br>")
            Else
                Dim b As Byte() = e.Result
                i.FileSize = 0
                i.Save()
                Response.Write(i.FileNameAndSize & "<br>")
            End If
            Response.Flush()
            m_count -= 1
        End SyncLock
    End Sub
#End Region

#Region "Event Handlers - UserControl"
    Private Sub ctrl_ExportClick() Handles ctrlFiles.ExportClick
        Files.ExportToCsv(Response, "Files.csv")
    End Sub
    Private Sub ctrl_ResortClick(ByVal sortBy As String, ByVal descending As Boolean, ByVal pageNumber As Integer) Handles ctrlFiles.ResortClick
        Response.Redirect(CSitemap.Files(txtSearch.Text, ExtensionId, New CPagingInfo(0, pageNumber - 1, sortBy, descending)))
    End Sub
#End Region

End Class
